#include <QSettings>
#include "externalcmdnotificator.h"
#include "externalcmdnotificatorconfig.h"

namespace Wishmaster
{
	ExternalCmdNotificator::ExternalCmdNotificator()
	{
		QSettings settings;
		m_cmd = settings.value("plugin/externalcmdnotificator/cmd", "notify-send --icon=\"%i\" \"%t\" \"%s\"").toString();
	}

	ExternalCmdNotificator::~ExternalCmdNotificator()
	{
	}

	bool ExternalCmdNotificator::notify(const Notification& notification)
	{
		QString cmd = m_cmd;
		cmd.replace("%t", QString(notification.title())).replace("%s", QString(notification.text())).replace("%i", QString(notification.icon())).replace("%s", QString(notification.soundFile()));
		m_process.start(cmd);
		return true;
	}

	QSharedPointer<QWidget> ExternalCmdNotificator::makeConfig()
	{
		QSharedPointer<ExternalCmdNotificatorConfig> config = QSharedPointer<ExternalCmdNotificatorConfig>(new ExternalCmdNotificatorConfig());
		config->setCmd(m_cmd);
		return config;
	}

	bool ExternalCmdNotificator::updateConfig(const QSharedPointer<QWidget>& config)
	{
		QSharedPointer<ExternalCmdNotificatorConfig> conf = config.dynamicCast<ExternalCmdNotificatorConfig>();
		if(!conf)
			return false;
		m_cmd = conf->getCmd();
		QSettings settings;
		settings.setValue("plugin/externalcmdnotificator/cmd", m_cmd);
		return true;
	}

	QString ExternalCmdNotificatorFactory::id()
	{
		return QString("External command");
	}

	QSharedPointer<Notificator> ExternalCmdNotificatorFactory::createNotificator()
	{
		return QSharedPointer<Notificator>(new ExternalCmdNotificator());
	}
}

Q_EXPORT_PLUGIN2(externalcmdnotificator, Wishmaster::ExternalCmdNotificatorFactory);
